<?

//echo '<pre>';
////print_r($arResult);
//print_r($arParams);
//echo '</pre>';

if (!CModule::IncludeModule("iblock")) {
    return;
}

$solutionId = $arResult["ID"];
$solution = CIBlockElement::GetByID($solutionId)->GetNext();

$schemeImageResult = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "SCHEME"));
$solution['SCHEME'] = CFile::GetFileArray($schemeImageResult->GetNext()['VALUE']);

$solution['DESCRIPTION'] = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "DESCRIPTION"))->GetNext();
$solution['EQUIPMENT'] = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "EQUIPMENT"))->GetNext();
$solution['OPTIONS'] = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "OPTIONS"))->GetNext();
$solution['PRICE'] = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "PRICE"))->GetNext();
$solution['SERVICE_PRICE'] = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "SERVICE_PRICE"))->GetNext();

$mapObjectResult = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "MAP_OBJECTS"))->GetNext();
$solution['MAP_OBJECTS'] = json_decode(str_replace('&quot;', '"', $mapObjectResult['VALUE']['TEXT']));

if ($solution) {
    $productsIds = array();
    $res = CIBlockElement::GetProperty(16, $solutionId, array("sort" => "asc"), Array("CODE" => "PRODUCTS"));
    while ($row = $res->GetNext()) {
        $productsIds[] = $row["VALUE"];
    }
    $solution['PRODUCTS'] = array();
    if (count($productsIds)) {
        $productsResult = CIBlockElement::GetList(
            array(),
            array(
                'IBLOCK_ID' => 3,
                'ID' => $productsIds
            )
        );

        /*$productsResult = CIBlockElement::GetList(
            Array("SORT" => "ASC"),
            array(
                'IBLOCK_ID' => 3,
                'ID' => $productsIds
            ),
            false, true,
            Array("ID", "PREVIEW_PICTURE", "DETAIL_PICTURE", "PROPERTIES")
        );*/

        /*while ($product = $productsResult->GetNext()) {
            $product["PREVIEW_PICTURE"] = CFile::GetFileArray($product["PREVIEW_PICTURE"]);
            $product["DETAIL_PICTURE"] = CFile::GetFileArray($product["DETAIL_PICTURE"]);

            $basePriceResult = CPrice::GetBasePrice($product['ID']);
            $product['PRICE'] = $basePriceResult['PRICE'];
            $product['CAN_BUY'] = $basePriceResult['CAN_BUY'];

            $product['PROPERTIES'] = $productsResult->GetNextElement()->GetProperties();

            $solution['PRODUCTS'][] = $product;

            echo '<pre>';
            print_r($product);
            echo '</pre>';
        }*/

        while ($res = $productsResult->GetNextElement()) {
            $product = $res->GetFields();

            $product["PREVIEW_PICTURE"] = CFile::GetFileArray($product["PREVIEW_PICTURE"]);
            $product["DETAIL_PICTURE"] = CFile::GetFileArray($product["DETAIL_PICTURE"]);

            $basePriceResult = CPrice::GetBasePrice($product['ID']);
            $product['PRICE'] = $basePriceResult['PRICE'];
            $product['CAN_BUY'] = $basePriceResult['CAN_BUY'];

            $product['PROPERTIES'] = $res->GetProperties();
            $product['QUANTITY'] = CCatalogProduct::GetByID($product['ID'])['QUANTITY'];

            $solution['PRODUCTS'][] = $product;

//            echo '<pre>';
//            print_r($product);
//            echo '</pre>';
        }

//        echo '<pre>';
//        print_r($arResult);
//        print_r($solution['QUANTITY']);
//        echo '</pre>';
    }
}

?>

<div style="width: 750px;">
    <h2 id="solution-page">РЕШЕНИЕ "<?= $solution['NAME'] ?>"</h2>
    <hr/>
    <label><?= $solution['SCHEME']['NAME'] ?></label>
    <img id="solution_scheme_canvas" src="<?= $solution['SCHEME']['SRC'] ?>" alt="Детальное изображение"
         usemap="#solution_scheme_map" style="opacity: 0"/>

    <map name="solution_scheme_map">
        <? foreach ($solution['MAP_OBJECTS'] as $area) { ?>
        <area shape="rect" productId="<?= $area->id ?>"
              coords="<?= $area->x1 ?>,<?= $area->y1 ?>,<?= $area->x2 ?>,<?= $area->y2 ?>"
              title="This is title"
              href="#"/>
        <? } ?>
    </map>

    <? foreach ($solution['PRODUCTS'] as $product) { ?>
    <script language="javascript" type="text/javascript">
        $(function () {
            $('area[productId="<?= $product['ID'] ?>"]').attr('title', '<?= $product['NAME'] ?>');
        });
    </script>
    <? } ?>

    <? foreach ($solution['PRODUCTS'] as $product) { ?>
    <div name="product_dialog" productId="<?= $product['ID'] ?>" productName="<?= $product['NAME'] ?>"
         style="display: none;">
        <?
        $APPLICATION->IncludeComponent(
            "bitrix:catalog.element",
            "solution-product-dialog",
            Array(
                "IBLOCK_TYPE" => "products",
                "IBLOCK_ID" => "3",
                "ELEMENT_ID" => $product['ID'],
                "ELEMENT_CODE" => "",
                "SECTION_ID" => $product["IBLOCK_SECTION_ID"],
                "SECTION_CODE" => "",
                "SECTION_URL" => "",
                "DETAIL_URL" => $product["DETAIL_PAGE_URL"],
                "BASKET_URL" => "/personal/basket.php",
                "ACTION_VARIABLE" => "action",
                "PRODUCT_ID_VARIABLE" => "id",
                "PRODUCT_QUANTITY_VARIABLE" => "quantity",
                "PRODUCT_PROPS_VARIABLE" => "prop",
                "SECTION_ID_VARIABLE" => "SECTION_ID",
                "META_KEYWORDS" => "-",
                "META_DESCRIPTION" => "-",
                "BROWSER_TITLE" => "-",
                "SET_TITLE" => "Y",
                "SET_STATUS_404" => "N",
                "ADD_SECTIONS_CHAIN" => "N",
                "PROPERTY_CODE" => array(),
                "OFFERS_FIELD_CODE" => array(),
                "OFFERS_PROPERTY_CODE" => array(),
                "OFFERS_SORT_FIELD" => "sort",
                "OFFERS_SORT_ORDER" => "asc",
                "OFFERS_SORT_FIELD2" => "name",
                "OFFERS_SORT_ORDER2" => "desc",
                "OFFERS_LIMIT" => "0",
                "PRICE_CODE" => array(),
                "USE_PRICE_COUNT" => "Y",
                "SHOW_PRICE_COUNT" => "1",
                "PRICE_VAT_INCLUDE" => "Y",
                "PRICE_VAT_SHOW_VALUE" => "Y",
                "PRODUCT_PROPERTIES" => $product['PROPERTIES'],
                "USE_PRODUCT_QUANTITY" => "Y",
                "LINK_IBLOCK_TYPE" => "",
                "LINK_IBLOCK_ID" => "",
                "LINK_PROPERTY_SID" => "",
                "LINK_ELEMENTS_URL" => "link.php?PARENT_ELEMENT_ID=#ELEMENT_ID#",
                "CACHE_TYPE" => $product["CACHE_TYPE"],
                "CACHE_TIME" => $product["CACHE_TYPE"],
                "CACHE_NOTES" => "",
                "CACHE_GROUPS" => "Y",
                "USE_ELEMENT_COUNTER" => "Y",
                "HIDE_NOT_AVAILABLE" => "N",
                "OFFERS_CART_PROPERTIES" => array(),
                "CONVERT_CURRENCY" => "Y",
                "CURRENCY_ID" => "RUB",
                "QUANTITY_FLOAT" => "N"
            )
        );?>
    </div>
    <? } ?>

    <div class="fleft" style="width: 65%;">
        <div id="product-description-block">
            <ul>
                <li><a href="#tab_description"><?=$solution['DESCRIPTION']['NAME']?></a></li>
                <li><a href="#tab_equipment"><?=$solution['EQUIPMENT']['NAME']?></a></li>
                <li><a href="#tab_options"><?=$solution['OPTIONS']['NAME']?></a></li>
            </ul>
            <div id="tab_description">
                <?=$solution['DESCRIPTION']['~VALUE']['TEXT']?>
            </div>
            <div id="tab_equipment">
                <?=$solution['EQUIPMENT']['~VALUE']['TEXT']?>
            </div>
            <div id="tab_options">
                <?=$solution['OPTIONS']['~VALUE']['TEXT']?>
            </div>
        </div>
    </div>
    <div class="fright">
        <div style="margin-top: 100px;">
            <p style="font-family: Arial; font-size: 1.1em;">
                <b><?=$solution['PRICE']['NAME']?></b>
                <br/>
                <span style="font-size: 1.4em; font-weight: bolder;">
                    <?= $solution['PRICE']['VALUE']?>
                </span>
                <br/>
                <b><?= $solution['SERVICE_PRICE']['NAME']?></b><br/>
                <span style="font-size: 1.4em; font-weight: bolder;">
                    <?=$solution['SERVICE_PRICE']['VALUE']?>
                </span>
                <br/>
            </p>

        </div>
    </div>
</div>